package com.ffcs.cn.action.base.checkbox;

import com.supermap.desktop.controls.utilities.ToolbarIconUtilities;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 带一个渲染文本的label和一个关闭按钮，作为渲染控件，是个抽象类，需要实现close方法，响应关闭按钮被点击到
 */
public abstract class PanelClose extends JPanel {

	private final ComboBoxSelectableDataItem comboBoxSelectableDataItem;
	private static final int IMAGE_SIZE = 18;

	public PanelClose(ComboBoxSelectableDataItem comboBoxSelectableDataItem) {
		this.comboBoxSelectableDataItem = comboBoxSelectableDataItem;
		JLabel labelText = new JLabel();
		labelText.setText(this.comboBoxSelectableDataItem.getValue());

		JButton buttonClose = new JButton(ToolbarIconUtilities.getIconByPath(ToolbarIconUtilities.CLOSE_24).getImage(IMAGE_SIZE,IMAGE_SIZE));
		buttonClose.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				comboBoxSelectableDataItem.setSelected(false);
				close(comboBoxSelectableDataItem);
			}
		});
		// 确保文字和关闭按钮同个大小，居中对齐
		labelText.setPreferredSize(new Dimension(labelText.getPreferredSize().width,IMAGE_SIZE));
		buttonClose.setPreferredSize(new Dimension(IMAGE_SIZE,IMAGE_SIZE));
		// 让button看起来像个label，不要有按钮框之类的
		buttonClose.setMargin(new Insets(0, 0, 0, 0));
		buttonClose.setContentAreaFilled(false);
		buttonClose.setBorderPainted(false);
		buttonClose.setOpaque(false);
		buttonClose.setBorder(BorderFactory.createEmptyBorder());

		setBorder(BorderFactory.createEmptyBorder());
		setLayout(new GridBagLayout());
		this.add(labelText, new GridBagConstraintsHelper(0, 0, 1, 1).setWeight(1, 1).setInsets(0, 0).setFill(GridBagConstraints.HORIZONTAL).setAnchor(GridBagConstraintsHelper.WEST));
		this.add(buttonClose, new GridBagConstraintsHelper(1, 0, 1, 1).setWeight(1, 1).setInsets(0, 0).setFill(GridBagConstraints.NONE).setAnchor(GridBagConstraintsHelper.EAST));
	}

	public ComboBoxSelectableDataItem getComboBoxDataItem() {
		return this.comboBoxSelectableDataItem;
	}

	protected abstract void close(ComboBoxSelectableDataItem comboBoxSelectableDataItem);
}
